#!/bin/bash
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -ex

if [ -z "$GIT_REPO" ]; then
    IVY_MIRROR_PROP=${IVY_MIRROR_PROP:-http://repo1.maven.org/maven2/}

    if [ -z "${MAVEN_ONLY}" ]; then
        TAR_TARGET="package package-native tar"
    else
        TAR_TARGET="jar bin-jar test-jar"
    fi

    if [ -n "${CAULDRON_BUILD}" ]; then
      IVYSETTINGS_OVERRIDE="-Divysettings.xml=${CAULDRON_TOP_DIR}/ivysettings.xml"
    else
      IVYSETTINGS_OVERRIDE=""
    fi

    ant -propertyfile cloudera/build.properties ${TAR_TARGET} -Dversion=${FULL_VERSION} \
      -Drepo.maven.org=${IVY_MIRROR_PROP} -Divy.home=${HOME}/.ivy2 \
      ${IVYSETTINGS_OVERRIDE} \
      "$@"

    if [ -n "$DO_MAVEN_DEPLOY" ]; then
# Change to cloudera/maven directory, and install
# (and if called from CDH nightly build, deploy) artifacts into Maven repository
        cd cloudera/maven-packaging
        mvn $CAULDRON_MAVEN_EXTRA_ARGS -Dnot.cdh.release.build=false -Dmaven.repo.local=${HOME}/.m2/repository${M2_REPO_SUFFIX} install $DO_MAVEN_DEPLOY
    fi
else
    mvn $CAULDRON_MAVEN_EXTRA_ARGS -f cloudera-pom.xml process-resources
    git add .
    git commit -a -m "Temporary commit for tarball"
    mkdir -p build
    git archive --prefix=zookeeper-${FULL_VERSION}/ --format=tar HEAD |gzip > build/zookeeper-${FULL_VERSION}.tar.gz
fi


